2 require_once 'modules/Accounts/Account.php';
3 require_once 'modules/Campaigns/Campaign.php';
4 require_once 'modules/CampaignLog/CampaignLog.php';
5 require_once 'modules/CampaignTrackers/CampaignTracker.php';
6 require_once 'modules/Contacts/Contact.php';
7 require_once 'modules/Users/User.php';
8 require_once 'include/SugarEmailAddress/SugarEmailAddress.php';
10 class CampaignLogTest extends Sugar_PHPUnit_Framework_TestCase
12 var $campaign_id = 'campaignforcamplogunittest';
13 var $prospect_list_id = 'prospectlistforcamplogunittest';
14 var $email_marketing_id = 'marketingforcamplogunittest';
15 var $targetObjectArray = array('User','Contact','Lead','Prospect','Account','CampaignTracker');
21 var $campaign_tracker;
25 public function setup()
29 $current_user = SugarTestUserUtilities::createAnonymousUser();
30 //for the purpose of this test, we need to create some records with fake campaign and prospect list data,
31 //however we do need to create some targets for the prospect list
33 //create campaign tracker
34 $ct = new CampaignTracker();
35 $ct->tracker_name ='Campaign Log Unit Test Tracker';
36 $ct->tracker_url = 'sugarcrm.com';
37 $ct->campaign_id = $this->campaign_id;
39 $this->campaign_tracker = $ct;
42 //for each type, create an object and populate the campaignLog list
43 foreach($this->targetObjectArray as $type){
44 //skip campaign tracker
45 if($type == 'CampaignTracker'){
51 if ($type == 'Account'){
52 $bean->name = 'CampLog Unit Test Account';
54 $bean->first_name = 'CampaignLog';
55 $bean->last_name = 'Test '.$type;
57 $type_obj = 'target_'.$type;
59 $this->$type_obj = $bean;
62 $sea = new SugarEmailAddress();
63 $id = $this->$type_obj->id;
64 $module = $this->$type_obj->module_dir;
65 $new_addrs=array();$primary='';$replyTo='';$invalid='';$optOut='';$in_workflow=false;
66 $_REQUEST[$module.'_email_widget_id'] = 0;
67 $_REQUEST[$module.'0emailAddress0'] = $type.'UnitTest@example.com';
68 $_REQUEST[$module.'emailAddressPrimaryFlag'] = '0emailAddress0';
69 $_REQUEST[$module.'emailAddressVerifiedFlag0'] = 'true';
70 $_REQUEST[$module.'emailAddressVerifiedValue0'] = 'unitTest@sugarcrm.com';
71 $requestVariablesSet = array('0emailAddress0','emailAddressPrimaryFlag','emailAddressVerifiedFlag0','emailAddressVerifiedValue0');
72 $sea->save($id, $module, $new_addrs, $primary, $replyTo, $invalid, $optOut, $in_workflow);
73 //unset email request values for next run
74 foreach ($requestVariablesSet as $k)
78 //now create the campaign log
79 $cl = new CampaignLog();
80 $cl->campaign_id = $this->campaign_id;
81 $cl->tracker_key = $ct->tracker_key;
82 $cl->target_id = $bean->id;
83 $cl->target_type = $bean->module_dir;
84 $cl->activity_type = 'targeted';//options are targeted (user was sent an email), link (user clicked on link), removed (user opted out) and viewed (viewed)
85 $cl->activity_date = date('Y-m-d H:i:s');
86 $cl->related_id = 'somebogusemailid'.date('His'); // this means link will not really work, but we are not testing email
87 $cl->related_type = 'Emails';
88 $cl->list_id = $this->prospect_list_id;
89 $cl->marketing_id = $this->email_marketing_id;
92 //keep last created campaign log bean to be used to call functions
93 $this->campaign_log = $cl;
98 public function tearDown()
100 global $current_user;
101 //for each type, delete the object and it's email
102 foreach($this->targetObjectArray as $type){
103 //skip campaign tracker
104 if($type == 'CampaignTracker'){
107 //create string to reference bean by
108 $type_obj = 'target_'.$type;
110 //remove the email address and relationship
111 $query = 'delete from email_addresses where email_address = \''.$type.'UnitTest@example.com\';';
112 $GLOBALS['db']->query($query);
113 $query = 'delete from email_addr_bean_rel where bean_id = \''.$this->$type_obj->id.'\';';
114 $GLOBALS['db']->query($query);
116 //remove the bean and delete record
117 $this->$type_obj->deleted = 1;
118 $this->$type_obj->save();
119 $GLOBALS['db']->query('DELETE FROM '.$this->$type_obj->table_name.' WHERE id = \''.$this->$type_obj->id.'\' ');
120 unset($this->$type_obj);
124 //delete the campaign logs and campaign tracker
125 $GLOBALS['db']->query('DELETE FROM campaign_log WHERE campaign_id = \''.$this->campaign_id.'\' ');
126 $GLOBALS['db']->query('DELETE FROM campaign_tracker WHERE id = \''.$this->campaign_tracker->id.'\' ');
127 unset($this->campaign_tracker);
128 unset($this->campaign_log );SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
129 unset($GLOBALS['current_user']);
134 public function testGetListViewData(){
135 global $current_user;
136 $lvd = $this->campaign_log->get_list_view_data();
138 //make sure the returned value is an array
139 $this->assertTrue(is_array($lvd), 'CampaignLog->get_list_view_data should return an object of array type');
141 //make sure some of the expected values exist
142 $this->assertFalse(empty($lvd['CAMPAIGN_ID']), 'array element CAMPAIGN_ID is expected to exist when calling CampaignLog->get_list_view_data ');
143 $this->assertFalse(empty($lvd['TARGET_ID']), 'array element TARGET_ID is expected to exist when calling CampaignLog->get_list_view_data ');
146 public function testGetRelatedName(){
147 global $current_user,$locale;
149 foreach($this->targetObjectArray as $type){
150 //skip campaign tracker
151 if($type == 'CampaignTracker'){
154 //create string to reference bean by
155 $type_obj = 'target_'.$type;
157 //make sure the related name is coming in from the correct related type
158 $related_name = $this->campaign_log->get_related_name($this->$type_obj->id,$this->$type_obj->module_dir);
160 //make sure the returned name is formatted as expected
161 if ($type == 'Account'){
162 $this->assertSame($related_name, $this->$type_obj->name, 'name retrieved from campaign log does not match the expected name of '.$formatted_name.' for the related '.$type.' object');
163 }elseif ($type == 'User'){
164 $formatted_name = $this->$type_obj->id.$this->$type_obj->module_dir;
165 $this->assertSame($related_name, $formatted_name, 'name retrieved from campaign log does not match the expected formatted name of '.$formatted_name.' for the related '.$type.' object');
167 $bean->first_name = 'CampaignLog';
168 $bean->last_name = 'Test '.$type;
169 $formatted_name = $locale->getLocaleFormattedName($this->$type_obj->first_name, $this->$type_obj->last_name);
170 $this->assertSame($related_name, $formatted_name, 'name retrieved from campaign log does not match the expected formatted name of '.$formatted_name.' for the related '.$type.' object');
177 public function testRetrieveEmailAddress(){
178 global $current_user;
179 foreach($this->targetObjectArray as $type){
180 //skip campaign tracker
181 if($type == 'CampaignTracker'){
184 //create string to reference bean by
185 $type_obj = 'target_'.$type;
187 $eastring = $this->campaign_log->retrieve_email_address($this->$type_obj->id);
188 $this->assertSame($eastring, $type.'UnitTest@example.com', 'email retrieved from campaign log object type '.$type.'does not match the expected email of '.$type.'UnitTest@example.com');